package com.kly.chatter.api.application.controller.chatter.res;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.kly.user.dto.chatter.response.ChatUser;
import com.kly.user.dto.chatter.response.ModelAccount;
import com.kly.user.dto.chatter.response.ModelsDto;
import com.kly.user.dto.chatter.response.UnReadMsgDto;
import lombok.Builder;
import lombok.Data;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
@Builder
public class ModelsRsp {

    /**
     * 新关系数
     */
    private Integer matchTotalNum;
    /**
     * model会话
     */
    private List<ChatUserVo> modelList;
    /**
     * 未读数
     */
    private Integer totalUnReadNum;

    private String news;

    public static ModelsRsp toModelRsp(ModelsDto dto) {
        if (Objects.isNull(dto)) {
            return ModelsRsp.builder().totalUnReadNum(0).build();
        }

        final List<ChatUser> modelList = dto.getModelList();
        List<ChatUserVo> models = Lists.newArrayList();
        for (ChatUser model : modelList) {
            models.add(ChatUserVo.builder().modelAccount(getModelAccountVo(model)).build());
        }
        return ModelsRsp.builder().totalUnReadNum(dto.getTotalUnReadNum()).matchTotalNum(dto.getMatchTotalNum()).modelList(models).build();
    }

    private static ModelAccountVo getModelAccountVo(ChatUser model) {
        final ModelAccount modelAccount = model.getModelAccount();
        final List<UnReadMsgDto> unreadDetail = modelAccount.getUnreadDetail();

        final ModelAccountVo vo = ModelAccountVo.builder().avatarUrl(modelAccount.getAvatarUrl()).gender(modelAccount.getGender())
                .nickName(modelAccount.getNickName()).online(modelAccount.getOnline())
                .targetId(modelAccount.getTargetId()).token(modelAccount.getToken())
                .unReadNum(modelAccount.getUnReadNum()).userCode(modelAccount.getUserCode())
                .showSayHiFlag(modelAccount.getShowSayHiFlag()).build();
        if (CollectionUtils.isNotEmpty(unreadDetail)) {
            vo.setUnreadDetail(unreadDetail.parallelStream().map(o -> UnReadMsgVo.builder().userCode(o.getUserCode()).unReadNum(o.getUnReadNum()).build()).collect(Collectors.toList()));
        }
        return vo;
    }


    public Integer getMatchTotalNum() {
        return matchTotalNum;
    }

    public ModelsRsp setMatchTotalNum(Integer matchTotalNum) {
        this.matchTotalNum = matchTotalNum;
        return this;
    }

    public List<ChatUserVo> getModelList() {
        return modelList;
    }

    public ModelsRsp setModelList(List<ChatUserVo> modelList) {
        this.modelList = modelList;
        return this;
    }

    public Integer getTotalUnReadNum() {
        return totalUnReadNum;
    }

    public ModelsRsp setTotalUnReadNum(Integer totalUnReadNum) {
        this.totalUnReadNum = totalUnReadNum;
        return this;
    }

    public String getNews() {
        return news;
    }

    public ModelsRsp setNews(String news) {
        this.news = news;
        return this;
    }
}
